﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class ModalOwnerControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }
        public void ShowModal(int companyid, int ownerid)
        {
            ResetFields();
            if (ownerid > 0)
            {
                LoadOwnerData(companyid, ownerid);
                hfOwnerID.Value = ownerid.ToString();
                btnDelete.Visible = true;
            }
            mpeModalOwnerControl.Show();
        }
        protected void btnModalOK_Click(Object sender, EventArgs e)
        {
            mpeModalOwnerControl.Hide();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int Ownerid = hfOwnerID.Value == "" ? 0 : Convert.ToInt32(hfOwnerID.Value);
                if (companyid > 0 && Ownerid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@OwnerID", SqlDbType.Int, 0, ParameterDirection.Input, Ownerid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblOwner WHERE OwnerID=@OwnerID AND CompanyId=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);

                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    lblMessage.Text = "Owner Deleted Successfully!";
                    btnSave.Visible = false;
                    btnDelete.Visible = false;
                }
            }
        }


        public void LoadOwnerData(int companyid, int ownerid)
        {
            hfOwnerID.Value = ownerid.ToString();
            if (ownerid > 0)
            {
                RSUserIdentity userIdentity = GetRSUserIdentity();
                String sql = "SELECT OwnerID,FirstName,LastName,MiddleName,Title,Mobile,Phone1,Phone2,Fax,Email,Street, " +
                                      "City,State,Zip,Country,Note,CompanyID FROM RSPRM.dbo.rs_tblOwner " +
                                      "WHERE OwnerID=@OwnerID AND CompanyID=@CompanyID";

                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sql, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@CompanyID";
                param.Value = userIdentity.RSCompanyId;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@OwnerID";
                param.Value = ownerid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();
                    txtEmail.Text = myDataReader["Email"].ToString();
                    txtFirstName.Text = myDataReader["FirstName"].ToString();
                    txtMiddleName.Text = myDataReader["MiddleName"].ToString();
                    txtMobile.Text = myDataReader["Mobile"].ToString();
                    txtLastName.Text = myDataReader["LastName"].ToString();
                    txtFax.Text = myDataReader["Fax"].ToString();
                    txtTitle.Text = myDataReader["Title"].ToString();
                    txtPhone1.Text = myDataReader["Phone1"].ToString();
                    txtPhone2.Text = myDataReader["Phone2"].ToString();
                    txtStreet.Text = myDataReader["Street"].ToString();
                    txtCity.Text = myDataReader["City"].ToString();
                    txtState.Text = myDataReader["State"].ToString();
                    txtZip.Text = myDataReader["Zip"].ToString();
                    txtCountry.Text = myDataReader["Country"].ToString();
                    txtNote.Text = myDataReader["Note"].ToString();
                }
                myConnection.Close();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int ownerid = int.Parse(hfOwnerID.Value);

                string title = txtTitle.Text;
                string firstname = txtFirstName.Text;
                string middlename = txtMiddleName.Text;
                string lastname = txtLastName.Text;
                string mobile = txtMobile.Text;
                string phone1 = txtPhone1.Text;
                string email = txtEmail.Text;
                string phone2 = txtPhone2.Text;
                string fax = txtFax.Text;
                string street = txtStreet.Text;
                string city = txtCity.Text;
                string state = txtState.Text;
                string zip = txtZip.Text;
                string country = txtCountry.Text;
                string note = txtNote.Text;

                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@FirstName", SqlDbType.NVarChar, 50, ParameterDirection.Input, firstname);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@LastName", SqlDbType.NVarChar, 50, ParameterDirection.Input, lastname);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@MiddleName", SqlDbType.NVarChar, 50, ParameterDirection.Input, middlename);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Title", SqlDbType.NVarChar, 15, ParameterDirection.Input, title);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Phone1", SqlDbType.NVarChar, 15, ParameterDirection.Input, phone1);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Phone2", SqlDbType.NVarChar, 15, ParameterDirection.Input, phone2);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Mobile", SqlDbType.NVarChar, 15, ParameterDirection.Input, mobile);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Fax", SqlDbType.NVarChar, 15, ParameterDirection.Input, fax);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Email", SqlDbType.NVarChar, 50, ParameterDirection.Input, email);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Street", SqlDbType.NVarChar, 150, ParameterDirection.Input, street);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@City", SqlDbType.NVarChar, 50, ParameterDirection.Input, city);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@State", SqlDbType.NVarChar, 50, ParameterDirection.Input, state);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Zip", SqlDbType.NVarChar, 10, ParameterDirection.Input, zip);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Country", SqlDbType.NVarChar, 50, ParameterDirection.Input, country);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Note", SqlDbType.NVarChar, 250, ParameterDirection.Input, note);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@OwnerID", SqlDbType.Int, 0, ParameterDirection.Input, ownerid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 30, ParameterDirection.Input, userIdentity.RSCurrentUser);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveOwner");
                SQLHelper.ExecuteScalarCmd(sqlCmd);

                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                lblMessage.Text = "Owner Saved Successfully!";
                btnSave.Visible = false;
                btnDelete.Visible = false;
            }
        }

        private void ResetFields()
        {
            txtEmail.Text = "";
            txtFirstName.Text = "";
            txtMiddleName.Text = "";
            txtMobile.Text = "";
            txtLastName.Text = "";
            txtFax.Text = "";
            txtTitle.Text = "";
            txtPhone1.Text = "";
            txtPhone2.Text = "";
            txtStreet.Text = "";
            txtCity.Text = "";
            txtState.Text = "";
            txtZip.Text = "";
            txtCountry.Text = "";
            txtNote.Text = "";
            btnSave.Visible = true;
            btnDelete.Visible = false;
        }
    }
}